﻿using Microsoft.EntityFrameworkCore;
using mozhi.smarterp.BasicData.EntityFrameworkCore;
using mozhi.smarterp.BasicData.Enums;
using mozhi.smarterp.BasicData.Products;
using mozhi.smarterp.BasicData.Products.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace mozhi.smarterp.BasicData.Repositories
{
    public class ProductRepository : EfCoreRepository<BasicDataDbContext, Product, Guid>, IProductRepository
    {
        public ProductRepository(IDbContextProvider<BasicDataDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        public async Task<int> GetCountAsync(string code, string name, Guid? excludeId, CancellationToken cancellationToken = default)
        {
            var dbContext = await GetDbContextAsync();

            int count = await dbContext.Set<Product>().AsNoTracking()
                .WhereIf(!code.IsNullOrEmpty(), x => x.Code == code)
                .WhereIf(!name.IsNullOrEmpty(), x => x.Name == name)
                .WhereIf(excludeId.HasValue, x => x.Id != excludeId)
                .CountAsync(cancellationToken);

            return count;
        }

        public async Task<int> GetCountAsync(string keyword, bool? activated, List<ProductType> types, bool? saleable, bool? purchasable, bool? producible, bool? outsourceable, bool? assetable, CancellationToken cancellationToken = default)
        {
            var dbContext = await GetDbContextAsync();

            int count = await dbContext.Set<Product>().AsNoTracking()
                .WhereIf(!keyword.IsNullOrEmpty(), x => x.Code.Contains(keyword) || x.Name.Contains(keyword))
                .WhereIf(activated.HasValue, x => x.Activated == activated)
                .WhereIf(types != null, x => types.Contains(x.Type))
                .WhereIf(purchasable.HasValue, x => x.Purchasable == purchasable)
                .WhereIf(saleable.HasValue, x => x.Saleable == saleable)
                .WhereIf(producible.HasValue, x => x.Producible == producible)
                .WhereIf(outsourceable.HasValue, x => x.Outsourceable == outsourceable)
                .WhereIf(assetable.HasValue, x => x.Assetable == assetable)
                .CountAsync(cancellationToken);

            return count;
        }

        public async Task<List<Product>> GetPagedListAsync(string keyword, bool? activated, List<ProductType> types, bool? saleable, bool? purchasable, bool? producible, bool? outsourceable, bool? assetable,
            int skipCount, int maxResultCount, string sorting, bool includeDetails = false, CancellationToken cancellationToken = default)
        {
            var dbContext = await GetDbContextAsync();

            var products = await dbContext.Set<Product>().AsNoTracking()
                .WhereIf(!keyword.IsNullOrEmpty(), x => x.Code.Contains(keyword) || x.Name.Contains(keyword))
                .WhereIf(activated.HasValue, x => x.Activated == activated)
                .WhereIf(types != null, x => types.Contains(x.Type))
                .WhereIf(purchasable.HasValue, x => x.Purchasable == purchasable)
                .WhereIf(saleable.HasValue, x => x.Saleable == saleable)
                .WhereIf(producible.HasValue, x => x.Producible == producible)
                .WhereIf(outsourceable.HasValue, x => x.Outsourceable == outsourceable)
                .WhereIf(assetable.HasValue, x => x.Assetable == assetable)
                .OrderByIf<Product, IQueryable<Product>>(!sorting.IsNullOrEmpty(), sorting)
                .PageBy(skipCount, maxResultCount)
                .ToListAsync(cancellationToken);

            return products;
        }
    }
}
